package com.su.dolphin.utils;

import java.io.File;

import android.os.Environment;
import android.os.StatFs;

/**
 * App的系统目录管理
 */
public class SDCardUtils
{
    public static final String FILE_BASE_APP_NAME = SystemUtil.getPackageName();
    public static final String FILE_TEMP_AVATAR_NAME = "file_temp_avatar_name.jpeg";
    public static final String PATH_ROOT = String.format("%s%s%s%s", getESDString(), "/.", FILE_BASE_APP_NAME, "/mobile/");
    public static final String PATH_TEMP = String.format("%stemp/", PATH_ROOT);// 临时缓存目录
    public static final String PATH_IMAGE = String.format("%simages/", PATH_ROOT);// 图像目录
    public static final String PATH_AUDIO = String.format("%saudio/", PATH_ROOT);// 音频目录
    public static final String PATH_FILE = String.format("%sfile/", PATH_ROOT);// 附件目录
    public static final String PATH_VCARD_AVANTAR = String.format("%savantars/", PATH_ROOT);// 名片头像目录
    public static final String PATH_STICKER = PATH_ROOT + "sticker/"; // sticker的路径
    public static final String PATH_THUMBNAIL = PATH_ROOT + "thumbnail/"; // 缩略图路径
    public static final String PATH_SAVE_IMAGE_NEW = String.format("%sCamera/", PATH_ROOT); // 在图片预览界面保存图片的路径
    public static final String PATH_GRAFFITI_SAVE = String.format("%simages/", PATH_ROOT); // 使用照相机功能图片的保存位置
    public static final String PATH_CRASH_LOG = String.format("%scrash/", PATH_ROOT); // 崩溃日志收集目录
    public static final String PATH_LOG = String.format("%slog/", PATH_ROOT); // 手动日志收集目录
    
    /**
     * 判断sd卡是否有效
     * 
     * @return
     */
    public static boolean getExternalStorageCard() {
        return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
    
    /**
     * 检查sd卡是否存储已满或者可用
     * 
     * @return
     */
    public static boolean diskSpaceAvailable() {
        File mSDCardDirectory = getESD();
        if (mSDCardDirectory == null) {
            return false;
        }
        StatFs fs = new StatFs(mSDCardDirectory.getAbsolutePath());
        // keep one free block
        return fs.getAvailableBlocks() > 1;
    }
    
    /**
     * 返回当前sdcard 可用空间大小， 单位byte
     */
    public static long getSdcardFreeSize() {
        // 取得SDCard当前的状态
        String sDcString = Environment.getExternalStorageState();
        long nSDFreeSize = 0;
        if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
            // 取得sdcard文件路径
            File pathFile = getESD();
            android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
            long nBlocSize = statfs.getBlockSize();
            long nAvailaBlock = statfs.getAvailableBlocks();
            nSDFreeSize = nAvailaBlock * nBlocSize;
        }
        return nSDFreeSize;
    }
    
    /**
     * 获取sd卡文件路径
     * 
     * @return
     */
    public static File getESD() {
        return Environment.getExternalStorageDirectory();
    }
    
    /**
     * 获取sd卡的路径
     * 
     * @return
     */
    public static String getESDString() {
        return getESD().toString();
    }
    
    /**
     * 初始化系统目录
     */
    public static void init() {
        try {
            // sd卡不存在的错误
            if (!getExternalStorageCard()) {
                return;
            }
            
            LogUtil.i(PATH_IMAGE);
            File imageFileDir = new File(PATH_IMAGE);
            File tempDir = new File(PATH_TEMP);
            File audioFileDir = new File(PATH_AUDIO);
            File fileFileDir = new File(PATH_FILE);
            File vcardAvatarDir = new File(PATH_VCARD_AVANTAR);
            File stickerDir = new File(PATH_STICKER);
            File saveImageDir = new File(PATH_SAVE_IMAGE_NEW);
            File graffitiSaveDir = new File(PATH_GRAFFITI_SAVE);
            File path_crash_log = new File(PATH_CRASH_LOG);
            File path_log = new File(PATH_LOG);
            
            if (!path_log.exists()) {
                path_log.mkdirs();
            }
            if (!imageFileDir.exists()) {
                imageFileDir.mkdirs();
            }
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            if (!audioFileDir.exists()) {
                audioFileDir.mkdirs();
            }
            if (!fileFileDir.exists()) {
                fileFileDir.mkdirs();
            }
            if (!vcardAvatarDir.exists()) {
                vcardAvatarDir.mkdirs();
            }
            if (!stickerDir.exists()) {
                stickerDir.mkdirs();
            }
            if (!saveImageDir.exists()) {
                saveImageDir.mkdirs();
            }
            if (!graffitiSaveDir.exists()) {
                graffitiSaveDir.mkdirs();
            }
            if (!path_crash_log.exists()) {
                path_crash_log.mkdirs();
            }
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
